<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="标签名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入标签名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList" row-key="id">
      <el-table-column label="序号" type="index" width="80"></el-table-column>
      <el-table-column prop="name" label="标签名称" min-width="260" show-overflow-tooltip></el-table-column>
      <el-table-column prop="" label="商品数量" width="260" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />   
    <CreateGoodsLabel v-if="isCreateHide" :isHide="isCreateHide" @update:isUpdateCreateHide="ev => isCreateHide = ev"
      :dataInfo="dataInfo" :titleName="titleName" @successAction="getList()" />
  </div>
</template>

<script setup>

import {
  GetGoodsTagList, // 标签列表查询
  RemoveGoodsTag, // 删除标签
} from "@/api/goodsManage/goodsLabel"
import { onMounted } from "vue";
import { ElMessageBox } from 'element-plus'

import CreateGoodsLabel from "./createGoodsLabel.vue"

const dataList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
let dataInfo = ref({})

let isCreateHide = ref(false)
let titleName = ref("新增")

const data = reactive({
  queryParams: {
    deptName: "",
    status: "",
    pageNum: 1,
      pageSize: 10,
  },
});
const total = ref(0)
const { queryParams, rules } = toRefs(data);

/** 查询标签列表 */
const getList = async () => {
  loading.value = true;
  try {
    let data = await GetGoodsTagList(queryParams.value)
    dataList.value = data.rows
    total.value = data.total
    loading.value = false;
  } catch (error) {
    loading.value = false;
  }

}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
}
/** 重置按钮操作 */
let queryRef = ref(null)
const resetQuery = () => {
  queryRef.value.resetFields();
  handleQuery();
}
/** 新增按钮操作 */
const handleAdd = (row) => {
  titleName.value = "新增";
  dataInfo.value = {}
  isCreateHide.value = true
}

/** 修改按钮操作 */
const handleUpdate = (row) => {
  titleName.value = "编辑";
  dataInfo.value = row
  isCreateHide.value = true
}

/** 删除按钮操作 */
const handleDelete = (row) => {
  ElMessageBox.confirm('是否确认删除名称为"' + row.name + '"的数据项?').then(() => {
    return RemoveGoodsTag([row.id]);
  }).then(() => {
    getList();
    ElMessageBox.msgSuccess("删除成功");
  }).catch(() => { });
}

onMounted(() => {
  getList();
})
</script>
